/**
 * @program: battery
 * @description: 多文件上传
 * @author: 王璐
 * @create: 2018-12-19 17:04
 **/

package com.aaa.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class Upload {

    public Map upload(MultipartFile file, String filePath) {
        //上传至服务器的路径
        String fileName= file.getOriginalFilename();
        //修改文件名称
        //获取文件后缀名称,拼接uuid
        String[] split = fileName.split("\\.");
        UUID uuid = UUID.randomUUID();
        fileName=uuid+"."+split[split.length-1];
        File targetFile= new File(filePath,fileName);
        //String src=filePath+fileName;
        Map map = new HashMap();
        map.put("name", fileName);
        map.put("url", "/"+fileName);
        try {
            //将页面传入的文件拷贝至服务器对应目录
            file.transferTo(targetFile);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }
}
